
#include <iostream>
#include "Entity.h"
#include "SB.h"
#include "hRandom.h"

int Entity::NEXT_ENTITY_ID = 1;

Entity::Entity()
{
    setCenter(Vector2(0.0f, 0.0f));
}

Entity::Entity(Vector2 _vPosition)
{
    setup(_vPosition);
}

void Entity::setup(Vector2 _vPosition)
{
    m_bRender = true;
    m_bUpdate = true;

    m_vPosition = _vPosition;
    m_fRotation = 0;
    m_vScale = Vector2(1.0f, 1.0f);
    m_vCenter = Vector2(0, 0);
    
    m_bLiving = false;
    m_bInitScaleRead = false;
    m_bLivingInit = false;
}

Entity::~Entity()
{
}

void Entity::update()
{
    if (m_bLiving)
    {
        if (!m_bInitScaleRead)
        {
            m_bInitScaleRead = true;
            m_vInitScale = getScale();
        }
        if (!m_bLivingInit)
        {
            m_bLivingInit = true;
            initLiving();
        }
        m_fLivingTime += SB::dt;
        
        float factorX = sinf((m_fLivingTime * m_vLivingSpeed.x) + m_vLivingOffset.x);
        float factorY = sinf((m_fLivingTime * m_vLivingSpeed.y) + m_vLivingOffset.y);
        setScale(m_vInitScale + Vector2(m_vInitScale.x * factorX * m_vLivingIntensity.x, m_vInitScale.y * factorY * m_vLivingIntensity.y));
    }
}

void Entity::render()
{
    render(Vector2(0.0f, 0.0f));
}

void Entity::render(Vector2 _vOffset)
{
}

void Entity::initLiving()
{
    m_fLivingTime = 0;
    
    m_vLivingOffset = Vector2(hRandom::getRandom(0, 10), hRandom::getRandom(0, 10));
    m_vLivingSpeed = Vector2(hRandom::getRandom(livingSpeedMin, livingSpeedMax), hRandom::getRandom(livingSpeedMin, livingSpeedMax));
    m_vLivingIntensity = Vector2(hRandom::getRandom(livingIntensityMin, livingIntensityMax), hRandom::getRandom(livingIntensityMin, livingIntensityMax));
}
